# .env/bin/python
import os
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager

from src import create_app, db

from src.models import user, auth, things, pay
# from flask_security.user_datastore import create_role, create_user, add_role_to_user
from src.utils.security import user_datastore
from flask_security.utils import encrypt_password as hash_password

'''
python manage.py runserver
python manage.py db init
python manage.py db migrate
python manage.py db upgrade
python manage.py db --help
python manage.py data
'''
app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)


@manager.command
def data():
    # init data
    user_datastore.find_or_create_role(
        name="admin", description="adminstrators")
    user_datastore.find_or_create_role(name="user", description="common user")
    admin = user_datastore.create_user(
        email='admin@local.melog',
        username='admin',
        active=True,
        password=hash_password('password')
    )
    user = user_datastore.create_user(
        email='user@local.melog',
        username='user',
        active=True,
        password=hash_password('password')
    )
    user_datastore.add_role_to_user(admin, 'user')
    user_datastore.add_role_to_user(user, 'user')
    user_datastore.add_role_to_user(admin, 'admin')
    db.session.commit()


if __name__ == "__main__":
    manager.run()
